package com.example.demo.service.impl;

import com.example.demo.entity.SpecialOffer;
import com.example.demo.dao.SpecialOfferDao;
import com.example.demo.service.SpecialOfferService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 优惠活动(SpecialOffer)表服务实现类
 *
 * @author makejava
 * @since 2022-01-12 18:22:02
 */
@Service("specialOfferService")
public class SpecialOfferServiceImpl implements SpecialOfferService {
    @Resource
    private SpecialOfferDao specialOfferDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SpecialOffer queryById(Integer id) {
        return this.specialOfferDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param specialOffer 筛选条件
     * @param pageRequest  分页对象
     * @return 查询结果
     */
    @Override
    public Page<SpecialOffer> queryByPage(SpecialOffer specialOffer, PageRequest pageRequest) {
        long total = this.specialOfferDao.count(specialOffer);
        return new PageImpl<>(this.specialOfferDao.queryAllByLimit(specialOffer, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param specialOffer 实例对象
     * @return 实例对象
     */
    @Override
    public SpecialOffer insert(SpecialOffer specialOffer) {
        this.specialOfferDao.insert(specialOffer);
        return specialOffer;
    }

    /**
     * 修改数据
     *
     * @param specialOffer 实例对象
     * @return 实例对象
     */
    @Override
    public SpecialOffer update(SpecialOffer specialOffer) {
        this.specialOfferDao.update(specialOffer);
        return this.queryById(specialOffer.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.specialOfferDao.deleteById(id) > 0;
    }
}
